package com.example.cd5160866.urlapp;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class Demo1Activity extends AppCompatActivity {
    @InjectView(R.id.expand_lv)
    ExpandableListView mainlistview;
    List<Parent> datas = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_1);
        ButterKnife.inject(this);
        initData();
        final MyAdapter adapter = new MyAdapter();
        mainlistview.setAdapter(adapter);
        mainlistview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(getApplicationContext(), groupPosition + " group " + childPosition + " chid", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        mainlistview.setDividerHeight(0);
    }

    // 初始化数据
    public void initData() {
        for (int i = 0; i < 15; i++) {
            List<Child> list = new ArrayList<>();
            for (int x = 3; x > 0; x--) {
                Child child = new Child();
                child.setData(x);
                list.add(child);
            }
            Parent parent = new Parent();
            parent.setData(i);
            if (i != 4)
                parent.setChilds(list);
            datas.add(parent);
        }
    }

    class MyAdapter extends BaseExpandableListAdapter {

        //得到子item需要关联的数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return datas.get(groupPosition).getChilds().get(childPosition);
        }

        //得到子item的ID
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //设置子item的组件
        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.test_item_child, null);
            }
            TextView tv = (TextView) convertView
                    .findViewById(R.id.tv_test_child);
            Child child = datas.get(groupPosition).getChilds().get(childPosition);
            tv.setText(child.getData() + "");
            return convertView;
        }

        //获取当前父item下的子item的个数
        @Override
        public int getChildrenCount(int groupPosition) {
            return datas.get(groupPosition).getChilds().size();
        }

        //获取当前父item的数据
        @Override
        public Object getGroup(int groupPosition) {
            return datas.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return datas.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //设置父item组件
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            ParentHolder holder;
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.test_item, null);
                holder = new ParentHolder();
                holder.textView = (TextView) convertView
                        .findViewById(R.id.tv_test);
                holder.arrow = (ImageView) convertView
                        .findViewById(R.id.iv_arrow);
                convertView.setTag(holder);
            } else {
                holder = (ParentHolder) convertView.getTag();
            }
            holder.textView.setText(datas.get(groupPosition).getData() + "");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                ObjectAnimator.ofFloat(holder.arrow, "rotation", holder.arrow.getRotation(), isExpanded ? 90f : 0f).setDuration(100).start();
            }
            return convertView;
        }


        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }

    public class ParentHolder {
        ImageView arrow;
        TextView textView;
    }

    public class Parent {
        private int data;
        private List<Child> childs = new ArrayList<>();

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public List<Child> getChilds() {
            return childs;
        }

        public void setChilds(List<Child> childs) {
            this.childs = childs;
        }
    }

    public class Child {
        private int data;

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }
    }

}
